package com.zhuoshi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zhuoshi.bean.AreaBean;
import com.zhuoshi.utils.DbHelper;

public class AreaDao {
	
	public int add(AreaBean bean){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("insert into area (areaname) values(?)");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setString(1, bean.getAreaname());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		return rows;
	}
	
	
	public List<AreaBean> getList(){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		List<AreaBean> list = new ArrayList<AreaBean>();
		
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from area");
			pstat = conn.prepareStatement(sql.toString());
			rs = pstat.executeQuery();
			while(rs.next()){
				AreaBean bean = new AreaBean();
				bean.setAreaname(rs.getString("areaname"));
				bean.setId(rs.getInt("id"));
				list.add(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return list;
	}
	
	public int update(AreaBean bean){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("update area set areaname=? where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setString(1, bean.getAreaname());
			pstat.setInt(2, bean.getId());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		
		return rows;
	}
	
	public int delete(int id){
		
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("delete from area where id=?");
			pstat = conn.prepareStatement(sql.toString());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		return rows;
	}
	
	public AreaBean getModel(int id){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		AreaBean bean = null;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from area where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			while(rs.next()){
				bean = new AreaBean();
				bean.setId(rs.getInt("id"));
				bean.setAreaname(rs.getString("areaname"));
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return bean;
	}
	

}
